<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

/**
 * Role information class.
 *
 * Role information's CRUD.
 */
class Chcode_params_model extends CI_Model {
	private $db_name = 'k9deploy_new';
	private $table = 'chcode_params';
	private $db = null;
	
	/**
	 * Constructor
	 *
	 * Sets the database used for
	 * information.
	 */
	public function __construct() {
		parent::__construct ();
		
		$this->db = $this->load->database ( $this->db_name, TRUE );
		
		$this->load->library ( 'memcached_lib' );
	}
	/**
	 * get enable chcode
	 * @param unknown $chcodes
	 */
	public function get_chcodes($chcodes) {
		if(empty($chcodes))
			return array();
		$query = $this->db->where_in ( 'chcode', $chcodes )->get ( $this->table );
		return $query->result_array ();
	}
	
	/**
	 * get params by chcodes
	 *
	 * @param unknown $chcodes        	
	 */
	public function get_params_by_chcodes($chcodes) {
		if (empty ( $chcodes ))
			return array ();
		
		if (is_array ( $chcodes )) {
			$query = $this->db->where_in ( 'chcode', $chcodes )->get ( $this->table );
			return $query->result_array ();
		} else {
			$where = array (
					'chcode' => $chcodes 
			);
			$query = $this->db->where ( $where )->get ( $this->table );
			return $query->row_array ();
		}
	}
	public function update_params($chcode, $params, $group_id, $iconflag) {
		$where = array (
				'chcode' => $chcode 
		);
		$data = array (
				'group_id' => $group_id,
				'params' => $params,
				'icon_flag' => $iconflag,
				'date' => time () 
		);
		
		$count = $this->db->where ( $where )->count_all_results ( $this->table );
		if ($count == 0) {
			$data = array_merge ( $where, $data );
			$this->db->insert ( $this->table, $data );
		} else {
			$this->db->where ( $where )->update ( $this->table, $data );
		}
	}
	/**
	 * get chcode list by group
	 * @param number $groupid
	 */
	public function get_chcode_list_by_group($groupid = 0){
		if($groupid == NULL){
				return array();
		}
		if($groupid != 0){
			$where = array('group_id' => $groupid);
			$this->db->where($where);
		}
		
		$query = $this->db->get($this->table);
		return $query->result_array();
	}
}